

export const throttle = <T extends (...args: any[]) => void>(func: T, wait: number = 200): (...args: Parameters<T>) => void => {
    let timeout: NodeJS.Timeout | null = null;
    let lastRun = 0;

    return function (this: ThisParameterType<T>, ...args: Parameters<T>) {
        const context = this;
        const now = Date.now();

        if (lastRun === 0 || now - lastRun >= wait) {
            func.apply(context, args);
            lastRun = now;
        }

        if (timeout === null) {
            timeout = setTimeout(() => {
                timeout = null;
                if (now - lastRun >= wait) {
                    func.apply(context, args);
                    lastRun = now;
                }
            }, wait);
        }
    };
};